<?php
    require_once('../code/global.php');
    require_once('/code/Admin.php');
    require_once('/code/Product.php');
    require_once('/code/Artist.php');
    require_once('/code/Category.php');
    $admin=new Admin();
    $admin->loginCheck(1);
    if(!isset($_GET['pid'])) {header('Location:items.php');exit;}
    $pid=intval($_GET['pid']);
    $cid=intval($_GET['cid']);
    $product=new Product();
    $p=$product->getProductByID($pid);
    if(empty($p)) {header('Location:items.php');exit;}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Xiaosha Sculpture 后台管理系统</title>
        <link rel="stylesheet" href="/styles/admin.css" type="text/css" media="all">
        <script type="text/javascript" src="/script/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="/script/script.js"></script>
        <script type="text/javascript">
            window.parent.scroll(0,0);
        </script>
    </head>
    <body>
        <?php
            if(isset($_GET['msg'])) echo '<div class="msgBox">'.$_GET['msg'];
            else echo '<div class="msgBox">编辑作品信息';
            echo ' <a href="items.php?cid='.$cid.'">返回</a></div>';
        ?>
        <table border="0" cellspacing="0" cellpadding="0" class="detailTable">
            <?php 
                $p_g=$product->getProductGallery($pid);
                foreach($p_g as $img){
            ?>
            <tr class="thumbTr"><td colspan="2">
            <div class="thumbDiv" style="background:url('<?php echo $img['thumb_url']; ?>') center no-repeat"></div><div class="descDiv"><span>Caption:</span><br /><?php echo $img['img_caption']; ?><br /><span>Description:</span><br /><?php echo nl2br($img['img_descript']); ?></div>
            <div class="btnDiv">
                <input class="btnLnk" type="button" value="Edit Image Info" onclick="location.href='editItemImage.php?iid=<?php echo $img['img_id']; ?>&cid=<?php echo $cid; ?>'">
                <?php
                    if($img['is_cover']) echo '<div>Cover Image</div>';
                    else{
                ?>
                    <form name="setCoverImage" action="exec.php" method="post">
                    <input type="hidden" name="action" value="setCoverImage">
                    <input type="hidden" name="img_id" value="<?php echo $img['img_id']; ?>">
                    <input type="hidden" name="category_id" value="<?php echo $cid; ?>">
                    <input type="hidden" name="product_id" value="<?php echo $pid; ?>">
                    <input class="btnLnk" type="submit" value="Set As Cover">
                    </form>
                    <form name="deleteGalleryImage" action="exec.php" method="post">
                    <input type="hidden" name="action" value="deleteGalleryImage">
                    <input type="hidden" name="img_id" value="<?php echo $img['img_id']; ?>">
                    <input type="hidden" name="category_id" value="<?php echo $cid; ?>">
                    <input type="hidden" name="product_id" value="<?php echo $pid; ?>">
                    <input class="btnLnk btnDel" type="submit" value="Delete Image">
                    </form>
                <?php
                    }
                ?>
            </div>
            </td></tr>
            <?php
                }
            ?>
            <tr><td>Add Image:</td><td>
                <form name="addGalleryImage" action="exec.php" method="post" enctype="multipart/form-data">
                <input type="hidden" name="action" value="addGalleryImage"><input type="hidden" name="max_file_size" value="10000000"><input type="hidden" name="product_id" value="<?php echo $pid; ?>"><input type="hidden" name="category_id" value="<?php echo $cid; ?>">
                <input type="file" name="imgFile"><input class="btnLnk" type="submit" id="btnUpload" value="Upload New Photo" onclick="wait()">
                </form>
            </td></tr>
            <tr class="seperator"><td colspan="2"></td></tr>
            <tr><td>Category:</td><td>
            <?php
                $p_c=$product->getProductCategories($pid);
                $is_only_cate=$product->countProductCategories($pid)<2;
                foreach($p_c as $c_name){
            ?>
                <form name="deleteProductCategory<?php echo $c_name['category_id']; ?>" action="exec.php" method="post">
                <input type="hidden" name="action" value="deleteProductCategory">
                <input type="hidden" name="product_id" value="<?php echo $p['product_id']; ?>">
                <input type="hidden" name="category_id" value="<?php echo $c_name['category_id']; ?>">
                <input type="hidden" name="cid" value="<?php echo $cid; ?>">
                    <div class="productCategory"><?php echo $c_name['name']; ?>[<input class="btnLnk" type="submit" name="delete" value="X"<?php if($is_only_cate) echo ' disabled'; ?>>]</div>
                </form>
            <?php
                }
            ?>
            </td></tr>
            <tr><td></td><td>
            <form name="addProductCategory" action="exec.php" method="post">
            <input type="hidden" name="action" value="addProductCategory">
            <input type="hidden" name="product_id" value="<?php echo $p['product_id']; ?>">
            <input type="hidden" name="cid" value="<?php echo $cid; ?>">
                <?php
                    $c=$product->getUnassignedCategories($pid);
                    $c_count=count($c);
                    echo '<select name="category_id"'.($c_count?'':' disabled').'>';
                    $i=0;
                    while($i<$c_count){
                        echo '<option value="'.$c[$i]['category_id'].'"'.($c[$i]['category_id']==$p['category_id']?' selected':'').'>'.$c[$i]['name'].'</option>';
                        ++$i;
                    }
                    echo '</select> <input class="btnLnk" type="submit" name="add" value="Add into Category"'.($c_count?'':' ').'>';
                ?>
            </form>
            </td></tr>
            <form name="editItem" action="exec.php" method="post">
            <input type="hidden" name="action" value="editItem">
            <input type="hidden" name="product_id" value="<?php echo $p['product_id']; ?>">
            <input type="hidden" name="category_id" value="<?php echo $cid; ?>">
            <tr><td>Subject:</td><td><input type="text" name="subject" maxlength="64" class="inputBox" value="<?php echo $p['subject']; ?>"></td></tr>
            <tr><td>Artist:</td><td>
                    <select name="artist_id">
                        <?php
                            $artist=new Artist();
                            $a=$artist->getArtists(0);
                            $a_count=count($a);
                            $j=0;
                            while($j<$a_count){
                                echo '<option value="'.$a[$j]['artist_id'].'"'.($a[$j]['artist_id']==$p['artist_id']?' selected':'').'>'.$a[$j]['name'].'</option>';
                                ++$j;
                            }
                        ?>
                    </select>
                </td></tr>
            <tr><td>Date:</td><td><input type="text" name="date" maxlength="64" class="inputBox" value="<?php echo $p['date']; ?>"></td></tr>
            <tr><td>Location:</td><td><input type="text" name="location" maxlength="64" class="inputBox" value="<?php echo $p['location']; ?>"></td></tr>
            <tr><td>Material:</td><td><input type="text" name="material" maxlength="64" class="inputBox" value="<?php echo $p['material']; ?>"></td></tr>
            <tr><td>Size:</td><td><input type="text" name="size" maxlength="64" class="inputBox" value="<?php echo $p['size']; ?>"></td></tr>
            <tr><td>Introduction:</td><td><textarea name="intro" class="mediumTxt"><?php echo $p['intro']; ?></textarea></td></tr>
            <tr><td>Code:</td><td><input type="text" name="code" maxlength="64" class="inputBox" value="<?php echo $p['code']; ?>"></td></tr>
            <tr><td>Price(￡):</td><td><input type="text" name="price" maxlength="64" class="inputBox" value="<?php echo $p['price']; ?>"></td></tr>
            <tr><td>Sold:</td><td><input name="is_sold" type="checkbox"<?php echo $p['is_sold']? 'checked':''; ?>></td></tr>
            <tr><td>Active:</td><td><input name="is_active" type="checkbox"<?php echo $p['is_active']? 'checked':''; ?>></td></tr>
            <tr><td>Created:</td><td><?php echo $p['create_time']?></td></tr>
            <tr><td>Modified:</td><td><?php echo $p['modify_time']?></td></tr>
            <tr class="btnTr header"><td colspan="2"><input class="btnLnk" type="submit" name="save" value="Save"><input class="btnLnk" type="reset" name="reset" value="Reset"></form><input class="btnLnk" type="button" name="cancel" value="Cancel" onclick="location.href='items.php?cid=<?php echo $cid; ?>'"></td></tr>
            <form name="deleteItem" action="exec.php" method="post"><input type="hidden" name="action" value="deleteItem"><input type="hidden" name="product_id" value="<?php echo $p['product_id']; ?>"><input type="hidden" name="category_id" value="<?php echo $cid; ?>">
            <tr class="btnTr header"><td colspan="2"><input class="btnLnk btnDel" type="submit" name="delete" value="Delete"></td></tr>
            </form>
        </table>
    </body>
</html>